package com.itheima.task;


import com.itheima.common.constant.RedisConstant;
import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Set;

/**
 * 处理套餐的定时任务
 */
@Component
@Slf4j
public class SetmealTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliOssUtil aliOssUtil;

    //处理垃圾图片  每天10点
 //   @Scheduled(cron = "0 0 10 1/1 * ?  ")  todo 代码测试通过，先关闭定时任务，后期需要时再打开
    public void dealPicture(){
        log.info("每天10点执行定时任务：处理垃圾图片"+ LocalDateTime.now());

        Set difference = redisTemplate.opsForSet().difference(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
       if(difference.isEmpty()) {
           difference.forEach(
                   picture -> {
                       System.out.println("需删除处理的图片：" + picture);

                       aliOssUtil.delete((String) picture);
                       System.out.println("删除redis中的数据");
                       redisTemplate.opsForSet().remove(RedisConstant.SETMEAL_PIC_RESOURCES, picture);

                       System.out.println(picture + "自动删除成功");
                   }
           );
       }else {
           log.info("没有需要处理的垃圾图片");
       }

    }
}
